home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / sonido / mod-0.000 / mod-0 / mod / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-04  |  3.7 KB  |  199 lines

  1. /*
  2.  *  file.c - Handles directory browsing.
  3.  *
  4.  *  (C) 1994 Mikael Nordqvist (d91mn@efd.lth.se, mech@df.lth.se)
  5.  */
  6.  
  7. #include <dirent.h>
  8. #include <sys/stat.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12.  
  13. #include "mod.h"
  14.  
  15. extern struct options opt;
  16. extern int nr_songs;
  17.  
  18. struct mod_file *files=0;   /* Array of files */
  19. char *file_names=0;         /* Space to store filenames in */
  20.  
  21. int nr_files; /* Includes directories */
  22. int nr_dirs;  /* Directories only     */
  23.  
  24. static char *tmp_workdir;   /* The dir we are reading */
  25.  
  26. /* Alpha-sort + directories at the top (with '../' last) */
  27.  
  28. int my_alphasort(const struct dirent * const *a,
  29.          const struct dirent * const *b)
  30. {
  31.     struct stat sa, sb;
  32.  
  33.     if(!stat((*a)->d_name, &sa) &&
  34.        !stat((*b)->d_name, &sb)) {
  35.     if((S_ISDIR(sa.st_mode) && S_ISDIR(sb.st_mode)) ||
  36.        (!S_ISDIR(sa.st_mode) && !S_ISDIR(sb.st_mode))) {
  37.         /* Make sure .. is last of the directories */
  38.         if(!strcmp("..", (*a)->d_name))
  39.         return 1;
  40.         else if(!strcmp("..", (*b)->d_name))
  41.         return -1;
  42.         else
  43.         return alphasort((void *)a, (void *)b);
  44.     }
  45.     else {
  46.         if(S_ISDIR(sa.st_mode))
  47.         return -1;
  48.         else
  49.         return 1;
  50.     }
  51.     }
  52.     else {
  53.     debug("Stat failed\n");
  54.     return alphasort((void *)a, (void *)b);
  55.     }
  56. }
  57.  
  58.  
  59. int choose_file(const struct dirent *de)
  60. {
  61.     if(!strcmp("/", tmp_workdir) && !strcmp("..", de->d_name))
  62.     return 0;
  63.     
  64.     if(!strcmp(".", de->d_name))
  65.     return 0;
  66.     else
  67.     return 1;
  68. }
  69.  
  70.  
  71. /* Read directory (workdir mus*/
  72.  
  73. void init_dir(char *dirname)
  74. {
  75.     int i, tmp;
  76.     struct stat s;
  77.     struct dirent **tmp_files;
  78.     char *name;
  79.  
  80.     tmp_workdir=dirname;
  81.  
  82.     if(files)
  83.     free(files);
  84.     if(file_names)
  85.     free(file_names);
  86.  
  87.     /* Should always be at least 1 file (..) */
  88.     if((nr_files=scandir(dirname, &tmp_files, choose_file, my_alphasort)) < 1)
  89.     error("scandir() failed.\n");
  90.     
  91.     files=(struct mod_file *)malloc(sizeof(struct mod_file)*nr_files);
  92.     
  93.     for(tmp=i=0; i < nr_files; ++i)
  94.     tmp+=1+strlen(tmp_files[i]->d_name);
  95.     
  96.     file_names=name=(char *)malloc(tmp);
  97.     
  98.     nr_dirs=0;
  99.     for(i=0; i < nr_files; ++i) {
  100.     strcpy(name, tmp_files[i]->d_name);
  101.     free(tmp_files[i]);
  102.     files[i].name=name;
  103.     files[i].options=-1; /* No options */
  104.     if(stat(name, &s)) {
  105.         files[i].size=-1;  /* Invalid file */
  106.         files[i].dir=0;
  107.     }
  108.     else {
  109.         if(S_ISDIR(s.st_mode)) {
  110.         files[i].dir=1;
  111.         nr_dirs++;
  112.         }
  113.         else
  114.         files[i].dir=0;
  115.         
  116.         files[i].size=s.st_size;
  117.     }
  118.     name+=1+strlen(name);
  119.     }
  120.     free(tmp_files);
  121. }
  122.  
  123.  
  124. /* Convert a play-list to a file-list */
  125.  
  126. void list_to_files(void)
  127. {
  128.     struct stat s;
  129.     int i;
  130.  
  131.     nr_files=nr_songs;
  132.     nr_dirs=0;
  133.     files=(struct mod_file *)malloc(sizeof(struct mod_file)*nr_files);
  134.     for(i=0; i < nr_files; ++i) {
  135.     files[i].name=get_fullmodulename_ptr(i);
  136.     files[i].dir=0;
  137.     files[i].options=1;
  138.  
  139.     if(stat(files[i].name, &s) < 0)
  140.         files[i].size=-1;  /* Invalid file */
  141.     else {
  142.         if(!S_ISDIR(s.st_mode))
  143.         files[i].size=s.st_size;
  144.         else
  145.         files[i].size=-1;
  146.     }
  147.     }
  148. }
  149.  
  150.  
  151. char *get_filestring(int nr)
  152. {
  153.     static char buf[81];
  154.  
  155.     if(files[nr].size >= 0) {
  156.     if(!files[nr].dir)
  157.         sprintf(buf, "%7d  ", files[nr].size);
  158.     else
  159.         strcpy(buf, "         ");
  160.     
  161.     strncat(buf, files[nr].name, 69);
  162.     
  163.     if(files[nr].dir)
  164.         strcat(buf, "/");
  165.     }
  166.     else {
  167.     strcpy(buf, "      -  ");
  168.     strncat(buf, files[nr].name, 69);
  169.     }
  170.     
  171.     return buf;
  172. }
  173.  
  174. /* Returns the filenumber of the specified directory name if it exists,
  175.  * otherwise zero.
  176.  */
  177.  
  178. int name_to_filenr(char *name)
  179. {
  180.     int i;
  181.     for(i=0; i < nr_files; ++i)
  182.     if(!strcmp(name, files[i].name) && files[i].dir)
  183.         return i;
  184.     return 0;
  185. }
  186.  
  187.  
  188. char *get_dirnamestring(int nr)
  189. {
  190.     return files[nr].name;
  191. }
  192.  
  193. /* For functions in other files */
  194.  
  195. int is_dir(int nr)
  196. {
  197.     return files[nr].dir;
  198. }
  199.